
import java.util.ArrayList;

public class e074 {
    public static void main(String[] args) {
        long sTime = System.currentTimeMillis();
        int num = 0;
        for (long i = 0; i < 1000000; i++) {
            if (stuckPattern(i) == 60) {
                num++;
            }
        }
        System.out.println(num);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }

    static int stuckPattern(long n) {
        long i = n;
        ArrayList<Long> h = new ArrayList<Long>();
        boolean is89 = false;
        h.add(i);
        while (true) {
            i = catFact(i);
            if (h.contains(i)) {
                break;
            }
            h.add(i);
        }
        h.add(i);
        return h.size()-1;
    }

    static long catFact(long n) {
        String s = String.valueOf(n);
        long sum = 0;
        for (int j = 0; j < s.length(); j++) {
            long k = factorial(Long.parseLong(String.valueOf(s.charAt(j))));
            sum += k;
        }
        return sum;
    }

    static long factorial(long n) {
        if (n == 0) {
            return 1;
        }
        long result = n;
        for (long i = n - 1; i > 0; i--) {
            result *= i;
        }
        return result;
    }
}